/**
 *    TimedProfiles - let Android perform tasks based on conditions
 *    Copyright (C) 2011  Daniel Weinhold
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * */
package de.drak.Profiles;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

/**Activity to select a Drawable as Icon for a Profile or Rule*/
public class IconPicker extends Activity
{    
    //---the images to display---
    Integer[] imageIDs = {
            R.drawable.icon_volume,
            R.drawable.icon_battery,
            R.drawable.icon_datepicker,
            R.drawable.icon_screen,
            R.drawable.icon_timepicker,
            R.drawable.icon_wireles,
            R.drawable.icon_tone,
            R.drawable.xtra1,
            R.drawable.xtra2,
            R.drawable.xtra3,
            R.drawable.xtra4,
            R.drawable.wipe,
            R.drawable.start,
            R.drawable.warn,
            R.drawable.time
    };
 
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.icon_picker);
 
        Gallery gallery = (Gallery) findViewById(R.id.gallery1);
 
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setSpacing(5);
        
        Button but = (Button) findViewById(R.id.Button01);
        but.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				
				Gallery gallery = (Gallery) findViewById(R.id.gallery1);
				if(gallery.getSelectedItemPosition()==AdapterView.INVALID_POSITION)
				{
					Toast.makeText(getBaseContext(), 
	                        "?", 
	                        Toast.LENGTH_SHORT).show();
					return;
				}
				
		    	Intent result = new Intent();
		    	result.putExtra("IconRes", imageIDs[gallery.getSelectedItemPosition()]);
				setResult(RESULT_OK, result);
				finish();
			}
		});
        but = (Button) findViewById(R.id.Button02);
        but.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				finish();
			}
		});
    }
 
    public class ImageAdapter extends BaseAdapter 
    {
        private Context context;
        private int itemBackground;
 
        public ImageAdapter(Context c) 
        {
            context = c;
            //---setting the style---
            /*TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
            itemBackground = a.getResourceId(
                R.styleable.Gallery1_android_galleryItemBackground, 0);
            a.recycle(); */                   
        }
 
        //---returns the number of images---
        public int getCount() {
            return imageIDs.length;
        }
 
        //---returns the ID of an item--- 
        public Object getItem(int position) {
            return position;
        }            
 
        public long getItemId(int position) {
            return position;
        }
 
        //---returns an ImageView view---
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(imageIDs[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(48, 48));
            imageView.setBackgroundResource(itemBackground);
            return imageView;
        }
    }    
}